<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>零库存转移记录 - 虚拟仓系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 统一导航栏 -->
    {% include 'includes/navbar.html' %}

    <div class="container mt-4">
        <h3>零库存转移记录（虚拟→自有）</h3>
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="input-group">
                    <select id="supplierFilter" class="form-select">
                        <option value="">所有供应商</option>
                    </select>
                    <input type="text" id="productIdFilter" class="form-control" placeholder="商品ID">
                    <button id="searchBtn" class="btn btn-primary">查询</button>
                    <button class="btn btn-success ms-2" data-bs-toggle="modal" data-bs-target="#transferModal">发起转移</button>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>转移单号</th>
                            <th>供应商</th>
                            <th>商品ID</th>
                            <th>商品名称</th>
                            <th>数量</th>
                            <th>时间</th>
                            <th>状态</th>
                        </tr>
                    </thead>
                    <tbody id="transferTable"></tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- 发起转移弹窗 -->
    <div class="modal fade" id="transferModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">虚拟库存→自有库存转移</h5>
                    <button class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="transferForm">
                        <div class="mb-3">
                            <label>供应商</label>
                            <select class="form-select" name="supplier_id" required id="transferSupplierSelect"></select>
                        </div>
                        <div class="mb-3">
                            <label>商品ID</label>
                            <input type="text" class="form-control" name="product_id" required>
                        </div>
                        <div class="mb-3">
                            <label>转移数量</label>
                            <input type="number" class="form-control" name="quantity" min="1" required>
                        </div>
                        <div class="mb-3">
                            <label>转入仓库</label>
                            <select class="form-select" name="warehouse" required>
                                <option value="华东仓">华东仓</option>
                                <option value="华南仓">华南仓</option>
                            </select>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button id="submitTransfer" class="btn btn-success">确认转移</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 加载供应商列表（带/api前缀）
        const loadSuppliers = async () => {
            const res = await fetch('/api/supplier/list');
            const suppliers = await res.json();
            const selectors = ['supplierFilter', 'transferSupplierSelect'];
            selectors.forEach(id => {
                const selector = document.getElementById(id);
                selector.innerHTML += suppliers.map(s => `<option value="${s.id}">${s.supplier_name}</option>`).join('');
            });
        };

        // 加载转移记录（带/api前缀）
        const loadTransferRecords = async () => {
            const supplierId = document.getElementById('supplierFilter').value;
            const productId = document.getElementById('productIdFilter').value;
            const res = await fetch(`/api/transfer/records?supplier_id=${encodeURIComponent(supplierId)}&product_id=${encodeURIComponent(productId)}`);
            const records = await res.json();
            const table = document.getElementById('transferTable');
            table.innerHTML = records.map(record => `
                <tr>
                    <td>${record.transfer_id}</td>
                    <td>${record.supplier_name}</td>
                    <td>${record.product_id}</td>
                    <td>${record.product_name}</td>
                    <td>${record.quantity}</td>
                    <td>${record.create_time}</td>
                    <td><span class="badge bg-success">${record.status}</span></td>
                </tr>
            `).join('');
        };

        // 发起转移（带/api前缀）
        document.getElementById('submitTransfer').addEventListener('click', async () => {
            const formData = Object.fromEntries(new FormData(document.getElementById('transferForm')));
            const res = await fetch('/api/transfer/virtual-to-own', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(formData)
            });
            const data = await res.json();
            alert(data.message + (data.transfer_id ? `\n转移单号：${data.transfer_id}` : ''));
            if (data.status === 'success') {
                document.getElementById('transferForm').reset();
                document.querySelector('#transferModal .btn-close').click();
                loadTransferRecords();
            }
        });

        document.getElementById('searchBtn').addEventListener('click', loadTransferRecords);
        document.getElementById('logout').addEventListener('click', () => window.location.href = '/login_page');
        window.onload = async () => {
            await loadSuppliers();
            loadTransferRecords();
        };
    </script>
</body>
</html>